<?php
namespace Admin\Controller;
use Think\Controller;

class FundingMergeController extends \Org\Util\AdminController {



    /**
     * 权限验证标志位,默认false
     * false:所有action都需要权限验证;
     * true:所有action都不需要权限验证;
     * array:array中的action不需要权限验证，其余需要。
     */
    protected $notAuthority = false;


    /**
     * 创建之后做的事情
     */
    public function interfaceAfterCreate($id) {
    }

    /**
     * 删除之前做的事情
     */
    public function interfaceBeforeDelete($id) {
    }

    /**
     * 更新之前做的事情
     */
    public function interfaceBeforeUpdate($id) {
    }

    /**
     * 父类中已经有典型的CRUD操作并预留入口
     * 如有较大改动则覆盖一下
     */
    public function create() {
        parent::create();
    }

    public function delete() {
        parent::delete();
    }

    public function update($id) {
        parent::update($id);
    }

    public function list($id) {
        //获取当前筹单信息
        $funding = D('Funding')->find($id);
        $fundingType = D('FundingType')->find($funding['funding_type']);
        $specifyModel = 'Funding'.ucfirst($fundingType['en']);
        $fundingSpecify = D($specifyModel)->where(['principle_id'=>$funding['id']])->find();
        //list出所有符合条件的
        //先依次从各个分表中查找符合条件的
        $searchStr = "id != {$funding['id']} AND funding_type = {$funding['funding_type']} AND transaction_type = '发起' AND is_togethor='是' AND state='众筹中'";
        $model = D('Funding');
        $count = $model->where($searchStr)->count();
        $page = new \Think\AdminPage($count, C('PAGE_NUM'));
        $show = $page->show();
        $list = $model->where($searchStr)->order('funding_type')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

    public function detail($id = 0) {
        parent::detail($id);
    }

    public function import() {
        parent::import();
    }

    public function export() {
        parent::export();
    }

    public function tableOpeationButton() {
        $return  = '
                                    <a class="" href="__MODULE__/FundingMerge/merge?id1={$_GET[id]}&id2={$vo[id]}" onclick="return confirm(\'是否确认合单？\');">
                                        <button class="btn btn-xs btn-success" style="border: 1px;">
                                            <i class="icon-beaker"></i>
                                        </button>
                                    </a>
        ';
        return $return;
    }

    public function merge($id1 = '', $id2='') {
        //从两个订单中找出价格低的作为主筹单
        $funding1 = D('Funding')->find($id1);
        $funding2 = D('Funding')->find($id2);
        $mainFundig = $funding1;
        $mergeFundig = $funding2;
        /*
        if ($funidng1['funding_price'] < $funding2['funding_price']) {
            $mainFundig = $funding1;
            $mergeFundig = $funding2;
        } else {
            $mainFundig = $funding2;
            $mergeFundig = $funding1;
        }
        */
        //把两个筹单都设置为众筹成功,并更新筹单订单
        D('Funding')->where(['id'=>$id1])->data(['state'=>'众筹成功'])->save();
        D('FundingOrder')->where(['funding_id' => $funding1['id']])->data(['state' => '待评价'])->save();
        D('Funding')->where(['id'=>$id2])->data(['state'=>'众筹成功'])->save();
        D('FundingOrder')->where(['funding_id' => $funding2['id']])->data(['state' => '待评价'])->save();
        //把附属单子的fundingorder都合并到主单里
        D('FundingOrder')->where(['funding_id' => $mergeFundig['id']])->data(['funding_id' => $mainFundig['id']])->save();
        $this->success('合单完成!', U('Funding/list'));
    }
}